﻿using System;
using System.Collections.Generic;
using System.Text;
using Eb;
using Ec;

public delegate void OnUCenterLogin(ClientLoginResponse client_login_response);

public class DefUCenterSDK4Client : ComponentDef
{
    //---------------------------------------------------------------------
    public override void defAllProp(Dictionary<string, string> map_param)
    {
    }
}

public class ClientUCenterSDK4Client<TDef> : Component<TDef> where TDef : DefUCenterSDK4Client, new()
{
    //-------------------------------------------------------------------------
    // key=acc, value=ClientUCenterSession
    Dictionary<string, Entity> mMapSession = new Dictionary<string, Entity>();

    //-------------------------------------------------------------------------
    public override void init()
    {
        EbLog.Note("ClientUCenterSDK4Client.init()");

        defNodeRpcMethod<ClientLoginResponse>(
            (ushort)_eUCenterMethodType.login2clientOnLogin, login2clientOnLogin);
    }

    //-------------------------------------------------------------------------
    public override void release()
    {
        EbLog.Note("ClientUCenterSDK4Client.release()");
    }

    //-------------------------------------------------------------------------
    public override void update(float elapsed_tm)
    {
    }

    //-------------------------------------------------------------------------
    public override void handleEvent(object sender, EntityEvent e)
    {
    }

    //-------------------------------------------------------------------------
    public void login(string ip, int port, string acc, string pwd, string project_name,
        string version_name, string channel_name, Dictionary<string, string> map_param,
        OnUCenterLogin cb_onlogin, float request_timeout = 10f)
    {
        if (string.IsNullOrEmpty(acc))
        {
            ClientLoginResponse client_login_response = new ClientLoginResponse();
            client_login_response.result = eLoginResult.accountNotExists;
            client_login_response.acc_id = 0;
            client_login_response.token = "";
            client_login_response.acc = acc;
            client_login_response.map_param = null;
            cb_onlogin(client_login_response);
            EbLog.Note("ClientUCenterSDK4Client.login para error");
            return;
        }

        if (mMapSession.ContainsKey(acc))
        {
            ClientLoginResponse client_login_response = new ClientLoginResponse();
            client_login_response.result = eLoginResult.loginstatus;
            client_login_response.acc_id = 0;
            client_login_response.token = "";
            client_login_response.acc = acc;
            client_login_response.map_param = null;
            cb_onlogin(client_login_response);
            EbLog.Note("ClientUCenterSDK4Client.login eLoginResult.loginstatus ");
            return;
        }

        ClientLoginRequest client_login_request = new ClientLoginRequest();
        client_login_request.acc = acc;
        client_login_request.pwd = pwd;
        client_login_request.project_name = project_name;
        client_login_request.channel_name = channel_name;
        client_login_request.version_name = version_name;
        client_login_request.map_param = map_param;

        Dictionary<string, object> cache_data = new Dictionary<string, object>();
        cache_data["ClientLoginRequest"] = client_login_request;
        cache_data["OnUCenterLogin"] = cb_onlogin;
        cache_data["LoginIp"] = ip;
        cache_data["LoginPort"] = port;
        cache_data["RequestTimeout"] = request_timeout;
        Entity et_session = EntityMgr.createEntityById<EtUCenterSession>(Guid.NewGuid().ToString(), cache_data, Entity);
        mMapSession[acc] = et_session;
    }

    //-------------------------------------------------------------------------
    // 响应登陆请求
    public void login2clientOnLogin(ClientLoginResponse client_login_response)
    {
        EbLog.Note("ClientUCenterSDK4Client.login2clientOnLogin()");

        Entity et_session = null;
        if (!mMapSession.TryGetValue(client_login_response.acc, out et_session))
        {
            // log error
            EbLog.Error("ClientUCenterSDK4Client.login2clientOnLogin() 没有找到acc=" + client_login_response.acc);
            return;
        }

        var co_session = et_session.getComponent<ClientUCenterSession<DefUCenterSession>>();
        co_session.login2clientOnLogin(client_login_response);
    }

    //-------------------------------------------------------------------------
    public void remove(string acc)
    {
        if (!string.IsNullOrEmpty(acc))
        {
            mMapSession.Remove(acc);
        }
    }
}
